//
//  QWConsultationController.m
//  qwm
//
//  Created by kevin on 2023/3/23.
//

#import "QWConsultationController.h"
#import "QWConsulationCell.h"
#import "QWConsDetalController.h"

static const int pageSize = 10;

@interface QWConsultationController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *showInfoArr;

@end

@implementation QWConsultationController
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
//    self.navigationController.navigationBarHidden = YES;
    if (@available(iOS 16.0, *)) {
        [self.leftBarButtonItem setHidden:YES];
    } else {
        self.leftBarButtonItem = nil;
    }
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"资讯";

    [self tableView];
    
    [SWHUDUtil showHudViewInSuperView:self.view withMessage:@"请等待"];
    [self loadPullDownRefreshDataInfo];
    
}

- (UITableView *)tableView{
    if(!_tableView){
        _tableView =[[UITableView alloc]initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.estimatedRowHeight = 400;
        _tableView.rowHeight = UITableViewAutomaticDimension;
        _tableView.estimatedSectionFooterHeight=0;
        _tableView.estimatedSectionHeaderHeight=0;
        _tableView.showsVerticalScrollIndicator=NO;
        _tableView.showsHorizontalScrollIndicator=NO;
        
        _tableView.delegate =self;
        _tableView.dataSource =self;
        
        [_tableView registerNib:[UINib nibWithNibName:@"QWConsulationCell" bundle:nil] forCellReuseIdentifier:@"QWConsulationCell"];
        
        _tableView.backgroundColor=Color_TableView_Gray;
        _tableView.separatorStyle=UITableViewCellSeparatorStyleNone;

        [_tableView addRefreshHeaderWithTarget:self action:@selector(loadPullDownRefreshDataInfo)];//下拉刷新
        [_tableView addLoadMoreFooterWithTarget:self action:@selector(loadPullUpRefreshDataInfo)];//上拉加载
        
        [self.view addSubview:_tableView];
        [_tableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.edges.equalTo(self.view);
        }];
        
    }
    return _tableView;
}
#pragma mark- UITableViewDelegate,UITableViewDataSource

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.0000001;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.0000001;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _showInfoArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    QWConsulationCell *cell = [tableView dequeueReusableCellWithIdentifier:@"QWConsulationCell"];
    QWConsDetailModel *model = _showInfoArr[indexPath.row];
    cell.nameLabel.text = model.title;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    QWConsDetalController *consDetailVC =[[QWConsDetalController alloc]init];
    QWConsDetailModel *model = _showInfoArr[indexPath.row];
    consDetailVC.detailModel = model;
    consDetailVC.hidesBottomBarWhenPushed = YES;
    [self.navigationController pushViewController:consDetailVC animated:YES];
}

#pragma mark- 下拉刷新，上拉加载
-(void)loadPullDownRefreshDataInfo
{
    NSDictionary *dic = @{
        @"pageNum": @1,
        @"pageSize": @(pageSize),
    };
    [HTTPManager sendRequestHttpDataUrl:URL_Health_List Dic:dic resMethod:JMP_HTTP_METHOD_GET success:^(JMPHttpResponse *response, NSInteger resStatusCode, NSString *errorMessage) {
        [self.showInfoArr removeAllObjects];
        NSArray *arr =response.rspData[@"list"];
        self->_showInfoArr =[QWConsDetailModel mj_objectArrayWithKeyValuesArray:arr];
        [self->_tableView reloadData];
        [self->_tableView judgeFooterState:arr.count<pageSize];
        [SWHUDUtil hideHudView];
    } failure:^(JMPHttpResponse *response, NSInteger resStatusCode, NSError *error) {
        [SWHUDUtil hideHudView];
    }];
}
-(void)loadPullUpRefreshDataInfo
{
    NSDictionary *dic = @{
        @"pageNum": @(_showInfoArr.count/pageSize),
        @"pageSize": @(pageSize),
    };
    [HTTPManager sendRequestHttpDataUrl:URL_Health_List Dic:dic resMethod:JMP_HTTP_METHOD_GET success:^(JMPHttpResponse *response, NSInteger resStatusCode, NSString *errorMessage) {
        NSArray *arr =response.rspData[@"list"];
        [self->_showInfoArr addObjectsFromArray:[QWConsDetailModel mj_objectArrayWithKeyValuesArray:arr]];
        [self->_tableView reloadData];
        [self->_tableView judgeFooterState:arr.count<pageSize];
        [SWHUDUtil hideHudView];
    } failure:^(JMPHttpResponse *response, NSInteger resStatusCode, NSError *error) {
        [SWHUDUtil hideHudView];
    }];
}
-(NSMutableArray *)showInfoArr
{
    if (!_showInfoArr) {
        _showInfoArr=[[NSMutableArray alloc]init];
    }
    return _showInfoArr;
}

@end
